package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DeptFileArchivedMapper;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.service.IDeptFileArchivedService;
import org.springframework.util.ObjectUtils;

/**
 * 部门文件归档Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-20
 */
@Service
public class DeptFileArchivedServiceImpl implements IDeptFileArchivedService
{
    @Autowired
    private DeptFileArchivedMapper deptFileArchivedMapper;


    /**
     * 查询部门文件归档
     *
     * @param associationId 关联业务id
     * @return 部门文件归档
     */
    @Override
    public DeptFileArchived selectDeptFileArchivedByAssociationId(String associationId){
        return deptFileArchivedMapper.selectDeptFileArchivedByAssociationId(associationId);
    }
    /**
     * 查询部门文件归档列表
     *
     * @param deptFileArchived 部门文件归档
     * @return 部门文件归档
     */
    @Override
    public List<DeptFileArchived> selectDeptFileArchivedList(DeptFileArchived deptFileArchived)
    {
        return deptFileArchivedMapper.selectDeptFileArchivedList(deptFileArchived);
    }

    /**
     * 新增部门文件归档
     *
     * @param deptFileArchived 部门文件归档
     * @return 结果
     */
    @Override
    public Long insertDeptFileArchived(DeptFileArchived deptFileArchived)
    {
        DeptFileArchived deptFileArchivedByFileName = deptFileArchivedMapper.selectDeptFileArchivedByFileName(deptFileArchived);
        if(!ObjectUtils.isEmpty(deptFileArchivedByFileName)){
            deptFileArchived.setFileName(deptFileArchived.getFileName()+"1");
        }
        DeptFileArchived deptFileArchivedParent = deptFileArchivedMapper.selectDeptFileArchivedByArchivedId(deptFileArchived.getParentId());
        if(ObjectUtils.isEmpty(deptFileArchivedParent)){
            throw new ServiceException("文件不存在，不允许新增");
        }
        if("0".equals(deptFileArchivedParent.getDelFlag())){
            throw new ServiceException("文件不存在，不允许新增");
        }
        deptFileArchived.setAncestors(deptFileArchivedParent.getAncestors() + "," + deptFileArchived.getParentId());
        deptFileArchived.setFilePath(deptFileArchived.getFilePath()+"/"+deptFileArchived.getFileName());
        deptFileArchived.setCreateTime(DateUtils.getNowDate());
        deptFileArchivedMapper.insertDeptFileArchived(deptFileArchived);
        Long archivedId = deptFileArchived.getArchivedId();
        return archivedId;
    }

    /**
     * 删除部门文件归档信息
     *
     * @param associationId 关联业务id
     * @return 结果
     */
    @Override
    public int deleteDeptFileArchivedByAssociationId(String associationId)
    {
        return deptFileArchivedMapper.deleteDeptFileArchivedByAssociationId(associationId);
    }
}
